package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSchoolTemplet;
import com.china08.yxyapi.entity.db.sws.SwsTempletModule;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSchoolTempletRepository;
import com.china08.yxyapi.repository.db.SwsTemplateModuleRepository;

@CrossOrigin
@RestController("SwsTemplateModuleSrhController")
public class SwsSchoolModuleSrhController extends YxyApiBaseController {

    @Autowired
    private SwsTemplateModuleRepository swsTemplateModuleRepository;

    @Autowired
    private SwsSchoolTempletRepository swsSchoolTempletRepository;

    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        RespModel respModel = new RespModel();
        String templateId = reqModel.templateId;
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.isTrue(StringUtils.isNotBlank(templateId), "模板ID为空");
        Assert.isTrue(StringUtils.isNotBlank(schoolId), "学校ID为空");

        List<SwsTempletModule> findByTemplateId = swsTemplateModuleRepository.findByTemplateId(templateId);

        List<SwsSchoolTemplet> templetModules = swsSchoolTempletRepository.findBySchoolIdAndTemplateId(schoolId, templateId);
        if (null != findByTemplateId || null != templetModules) {
            List<String> moduleIds = new ArrayList<String>();
            for (SwsTempletModule templetModule : findByTemplateId) {
                moduleIds.add(templetModule.getModuleId());
            }
            List<String> templetModuleids = new ArrayList<String>();

            for (SwsSchoolTemplet schoolTemplet : templetModules) {
                templetModuleids.add(schoolTemplet.getModuleId());
            }
            respModel.moduleIdList = moduleIds;
            respModel.SchoolTempletIdList = templetModuleids;
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String templateId;
    }

    protected class RespModel extends YxyApiRespModel {

        public List<String> moduleIdList;

        public List<String> SchoolTempletIdList;

    }

}
